import time
from typing import Callable, List
from xml.etree import ElementTree as ET


def get_xml_etree_value(dom: ET.Element, node: str):
    if node.startswith('/'):
        node = node[1:]
    elif node.startswith('//'):
        node = node[2:]
    el_list: List[ET.Element] = dom.findall(f'*//{node}')
    if not el_list:
        value = None
    else:
        value = el_list[0].text
    return value


def get_json_node_value_by_path(j, path:str, join='.'):
    obj = j
    for name in path.split(join):
        obj = obj[name]
    return obj


def check_expected(expected: str, real: str):
    pass


def wait_func_true(func: Callable, time_out, ignore_error=False, interval=1, *args, **kwargs):
    t = time.time()
    while True:
        try:
            r = func(*args, **kwargs)
        except:
            if ignore_error:
                if time.time() - t > time_out:
                    raise TimeoutError(f'{time_out}秒等待超时')
                time.sleep(interval)
                continue
            else:
                raise
        if r:
            break


if __name__ == '__main__':
    v = get_json_node_value_by_path({
        '1':{
            '2':{
                '3':'333'
            }
        }
    }, path='1.2', join='.')
    print(v)


